//
//  GCMGongZuoRiHeViewController.m
//  GeeCityManager
//
//  Created by 聚城科技 on 16/8/9.
//  Copyright © 2016年 聚城科技. All rights reserved.
//

#import "GCMGongZuoRiHeViewController.h"
#import "GCMQJXMGLViewController.h"
#import "GCMGZRHTableViewCell.h"
#import "GCMQualityGZRHRequst.h"

@interface GCMGongZuoRiHeViewController ()<UITableViewDelegate,UITableViewDataSource,UIAlertViewDelegate>

@end

@implementation GCMGongZuoRiHeViewController{
    
    NSMutableArray *_dataSource;
    UITableView *_tableV;
    NSInteger _currentPage;
    NSMutableArray *_QjibieArray;
    NSMutableArray *_idArr;
    NSMutableArray *_nameArr;
    NSArray *_titleArr;

    
    
}
-(void)viewWillAppear:(BOOL)animated{
    
    self.navigationController.navigationBarHidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createUI];
    _currentPage = 1;
    [self createTableView];
    [self downloadTableViewData];
    //    下拉刷新
    _tableV.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _currentPage = 1;
        // 进入刷新状态后会自动调用这个block
        [self downloadTableViewData];
        [_tableV.mj_header endRefreshing];
        
    }];
    //    上拉加载
    _tableV.mj_footer  = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        // 进入加载状态后会自动调用这个block
        _currentPage ++;
        NSString *pageStr  = [NSString stringWithFormat:@"%ld",_currentPage];
        [self downloadNextPageTableViewDataWith:pageStr];
        [_tableV.mj_footer endRefreshing];
    }];
    
}

-(void)createUI{

    self.view.backgroundColor = [UIColor colorWithRed:0.93 green:0.93 blue:0.93 alpha:1.0];
    if ([self.type isEqualToString:@"一天一次"]) {
        
        _titleArr  = @[@"日期",@"得分/(分)",@"状态"];
        
    }else{
    
        _titleArr  = @[@"日期",@"上午/(分)",@"下午/(分)",@"状态"];
    }
    
    for (int i = 0; i < _titleArr.count; i ++) {
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake((SCREEN_WIDTH - _titleArr.count - 1)/_titleArr.count*i + i, 64, (SCREEN_WIDTH - _titleArr.count - 1)/_titleArr.count, 50)];
        label.text = _titleArr[i];
        label.textAlignment  = NSTextAlignmentCenter;
        label.backgroundColor = [UIColor whiteColor];
        if (i > 0) {
            label.textColor = [UIColor colorWithRed:0 green:0.62 blue:0.51 alpha:1.0];
        }
        [self.view addSubview:label];
    }
    
}

-(void)createTableView{
    _tableV = [[UITableView alloc]initWithFrame:CGRectMake(0,114, SCREEN_WIDTH, SCREEN_HEIGHT - 114)];
//    _tableV.backgroundColor  = [UIColor redColor];
    _tableV.delegate = self;
    _tableV.dataSource =self;
    _tableV.backgroundColor = [UIColor colorWithRed:0.93 green:0.93 blue:0.93 alpha:1.0];
    [self.view addSubview:_tableV];
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    GCMGZRHTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[GCMGZRHTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    [cell setBackgroundColor:[UIColor colorWithRed:.93 green:.93 blue:.93 alpha:1]];
//    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    //    cell.textLabel.textColor = [UIColor colorWithRed:0.39 green:0.39 blue:0.39 alpha:1];
    NSDictionary *dic = _dataSource[indexPath.row];
    NSLog(@"--------%@",dic);
    [cell setDic:dic];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSource.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 51;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *timestr = [NSString stringWithFormat:@"%@",[NSDate date]];
    NSArray *array = [timestr componentsSeparatedByString:@" "];
    int i = [self compareDate:array[0] withDate:[_dataSource[indexPath.row] valueForKey:@"riqi"]];
//    NSLog(@"%d",i);
    if (i != 1) {
        GCMQJXMGLViewController * reciveVC = [[GCMQJXMGLViewController alloc]init];
        reciveVC.a_id = self.a_id;
        reciveVC.ht_id = self.ht_id;
        reciveVC.jibie_id = self.jiBie_id;
        reciveVC.type = self.type;
        reciveVC.date = [_dataSource[indexPath.row] valueForKey:@"riqi"];
        [self.navigationController pushViewController:reciveVC animated:YES ];
        UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
        backItem.title = @"品质检查";
        self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
        self.navigationItem.backBarButtonItem = backItem;
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"您的打分不能超过当前日期，请改天再来" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];        
        alert.delegate = self;
        [alert show];
    }
}



//比较两个日期的大小

-(int)compareDate:(NSString*)date01 withDate:(NSString*)date02{
    int ci;
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"yyyy-MM-dd"];
    NSDate *dt1 = [[NSDate alloc] init];
    NSDate *dt2 = [[NSDate alloc] init];
    dt1 = [df dateFromString:date01];
    dt2 = [df dateFromString:date02];
    NSComparisonResult result = [dt1 compare:dt2];
    switch (result)
    {
            //date02比date01大
        case NSOrderedAscending: ci=1; break;
            //date02比date01小
        case NSOrderedDescending: ci=-1; break;
            //date02=date01
        case NSOrderedSame: ci=0; break;
        default: NSLog(@"erorr dates %@, %@", dt2, dt1);
        break;
    }
    return ci;
}

//初始化数据

-(void)initData{
    
}

-(void)downloadTableViewData{
    
    
//    NSLog(@"+++%@",self.data);
    [GCMQualityGZRHRequst postAreaDataWithAid:self.a_id WithHid:self.ht_id WithJiBieid:self.jiBie_id WithType:self.type WithPage:@"1" WithData:self.data WithUrl:QUALISTGZRH_URL Success:^(id obj, NSError *err) {
        [_dataSource removeAllObjects];
        _dataSource = obj;
        [_tableV reloadData];
    }];
}
-(void)downloadNextPageTableViewDataWith:(NSString*)pageStr{

    [GCMQualityGZRHRequst postAreaDataWithAid:self.a_id WithHid:self.ht_id WithJiBieid:self.jiBie_id WithType:self.type WithPage:pageStr WithData:self.data WithUrl:QUALISTGZRH_URL Success:^(id obj, NSError *err) {
        [_dataSource addObjectsFromArray:obj];
        [_tableV reloadData];
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
